<?php
namespace Systemht\Model;
use Think\Model;
class AdminModel extends Model{
	protected $insertField = array('username','password','group_id','name');
	protected $updateField = array('id','username','password','group_id','name');
	protected $_validate = array(
		array('username','require','请填写管理员名称'),
		array('password','require','请填写密码'),
		array('username','','该用户名已存在',0,'unique',1),
		array('group_id','0','请选择角色',1,'notequal'),
	);
	protected function _before_insert(&$data,$option){
		$data['ctime'] = time();
	}
	protected function _after_insert(&$data,$option){
		M('auth_group_access')->add(array(
			'uid' => $data['id'],
			'group_id' => I('post.group_id')
		));
	}
	protected function _after_update(&$data,$option){
		$model = M('auth_group_access');
		$model->where(array('uid'=>$option['where']['id']))->delete();
		$model->add(array(
			'uid' => $option['where']['id'],
			'group_id' => I('post.group_id')
		));
	}
}